﻿<?php
    //定义数据库连接参数
    define("SERVERNAME", "localhost");
    define("USERNAME", "root");
    define("PASSWORD", "ldh02211012");
    define("DBNAME", "piggydb");
    define("SPLIT", "@");
    
    function select($tb){
        // 创建连接
        $conn = new mysqli(SERVERNAME, USERNAME, PASSWORD, DBNAME);
        // 检测连接
        if ($conn->connect_error) {
            die("连接失败: " . $conn->connect_error);
        } 
        $sql = "SELECT * FROM ".$tb;
        $result = $conn->query($sql);
        return $result;
    }
    
    //今日收入支出
    function today($type){
        if(!($type==0 || $type==1 || $type==2))
            return ;
            
        // 创建连接
        $conn = new mysqli(SERVERNAME, USERNAME, PASSWORD, DBNAME);
    
        // 检测连接
        if ($conn->connect_error) {
            die("连接失败: " . $conn->connect_error);
        } 
        //执行
        if($type==0){
            $sql = "SELECT * FROM `ACCOUNT` WHERE to_days(date)=to_days(now()) and money<0 ORDER BY date DESC";
        }elseif ($type==1){
           $sql = "SELECT * FROM `ACCOUNT` WHERE to_days(date)=to_days(now()) and money>0 ORDER BY date DESC";
        }elseif ($type==2) {
           $sql = "SELECT * FROM `ACCOUNT` ORDER BY date DESC";
        }
        $data = $conn->query($sql);
        $contents=array();//记录内容
        if ($data->num_rows > 0) {
            // 输出每行数据
            while($row = $data->fetch_assoc()) {
                $c= "时间: ". substr($row["date"],-8).'<br>'.
                    "金额: ". $row["money"].'<br>'.
                    "类别: ". handleType($row["type"]).'<br>'.
                    "详情: ". $row["details"].'<br>';
                array_push($contents,$c);
            }           
        } 
        return $contents;
    }
    
    //处理结果  data:要处理的数据   flag:是否统计
    function handle($data,$flag){
        if(count($data)<=0){
            return '0条记录<br>';
        }
        
        $countOutM=0.00;//总支出
        $countInM=0.00; //总收入
        $maxM=0.00;     //最高支出
        $maxC="";       //最高支出详情
        $result="";     //存放结果
        
        $m=array();
        $i=array();
        
        foreach ($data as $key => $value) {
            $cM=(double)getData($value,'金额:');//当前金额
            //信息记录
            if($cM>0){
                $countInM=$countInM+$cM;
            }else{
                $countOutM=$countOutM-$cM;
            }
            if($cM>$maxM){
                $maxM=$cM;
                $maxC=getData($value,'详情:');
            }
            
            $result= $result.$value.'<br>'; 
        }
        
        if($flag){//是否统计
            $cc='今日收录：<'.count($data).'> 条记录<br>'.
                '总计支出：['.$countOutM.'元] | 总计收入：['.$countInM.'元] <br>'.
                '其中最高支出为：'.$maxC.'['.$maxM.'元] <br>'.
                '当前余额为：[300元]';
            $result=$result.$cc;
        }
        
        return $result;
    }
    
    //类别处理
    function handleType($type){
        if($type==1){
            return '借/还';
        }else{
            return '收/支';
        }
    }
    
    //字符串取数据
    function getData($data,$str){
         $star = strpos($data,$str);
         $end = strpos($data,'<br>', $star);
         return substr($data,$star+7,$end-$star-7);
    }
    
    
    //添加一条记录
    function addAccount($data){
        $arr=explode(SPLIT,$data);
        $c=count($arr);
        if($c<2 ||$c>3)
            return -1;
            
        $date=date('y-m-d h:i:s',time());
        $money=$arr[0];
        
        if(strlen($money)<=0)
            return -1;
            
        $details=$arr[1];
        if(strlen($details)<=0)
            return -1;
        
           
        if($c==3){
            $type=$arr[2];
            if(strlen($type)<=0)
                return -1;
        }else{
            $type=0;
        }
        
         // 创建连接
        $conn = new mysqli(SERVERNAME, USERNAME, PASSWORD, DBNAME);
    
        // 检测连接
        if ($conn->connect_error) {
            die("连接失败: " . $conn->connect_error);
        }
        $sql="INSERT INTO `account`( `uid`, `money`, `date`, `details`, `type`) VALUES (1,'".$money."','".$date."','".$details."','".$type."')";
        return $conn->query($sql);
        
    }
    
    //撤消上一条记录
    function removeNewly(){
         // 创建连接
        $conn = new mysqli(SERVERNAME, USERNAME, PASSWORD, DBNAME);
    
        // 检测连接
        if ($conn->connect_error) {
            die("连接失败: " . $conn->connect_error);
        }
        $sql='delete from account where 1 order by date ASC limit 1';
        $conn->query($sql);
        
    }
    
    //显示上一条记录
    function showNewly(){
        // 创建连接
        $conn = new mysqli(SERVERNAME, USERNAME, PASSWORD, DBNAME);
		
        // 检测连接
        if ($conn->connect_error) {
            die("连接失败: " . $conn->connect_error);
        }
        $sql='select * from account where 1 order by date ASC limit 1';
        $data=$conn->query($sql);
		return count($data);
		if(count($data)<=0)
			return '当前没有记录';
			
        while($row = $data->fetch_assoc()) {
			return 'aaaaaaaa';
            $c= "时间: ". $row["date"].'<br>'.
                "金额: ". $row["money"].'<br>'.
                "类别: ". handleType($row["type"]).'<br>'.
                "详情: ". $row["details"].'<br>';
            return $c;
        }     
		return 'aaaaaaaa';
        
         
    }
    
    //计算
    function reckon($data){
        $num=preg_split('/[-+\*]/is', $data);
        $note=preg_split('/[^-+\*]+/', $data);
        print_r($num);
    }
	
	//新用户
    function addUser($oId,$money){
        // 创建连接
        $conn = new mysqli(SERVERNAME, USERNAME, PASSWORD, DBNAME);
    
        // 检测连接
        if ($conn->connect_error) {
            die("连接失败: " . $conn->connect_error);
        }
        $sql="INSERT INTO `user`(`openid`, `money`) VALUES ('".$oId."','".$money."')";
        $conn->query($sql);
    }
	
    
    
    
   
?>